<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/plat-style.css" media="all">
    <style>
        thead tr th {
            text-align: center !important;
        }

        tr td {
            text-align: center !important;
        }

    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
				<span class="layui-breadcrumb" lay-filter="breadcrumb">
	              <a href="javascript:;">鸡舍管理</a>
	              <a><cite>待分红</cite></a>
	            </span>

                </div>
                <div class="layui-card-body layui-form">
                    <%-- 查询条件设置 --%>
                    <div class="layui-container layui-form-item" style="padding-bottom: 50px">
                        <div class="layui-col-md12">
                            <div class="layui-col-md3">
                                <div class="layui-col-sm4" style="line-height: 38px">用户昵称：</div>
                                <div class="layui-col-sm7">
                                    <input class="layui-input" name="nickName" autocomplete="off">
                                </div>
                            </div>
                            <div class="layui-col-md3">
                                <div class="layui-col-sm3" style="line-height: 38px">订单编号：</div>
                                <div class="layui-col-sm7">
                                    <input class="layui-input" name="orderCode" autocomplete="off" value="${orderCode}">
                                </div>
                            </div>
                            <div class="layui-col-md3">
                                <div class="layui-col-sm3" style="line-height: 38px">会员等级：</div>
                                <div class="layui-col-md7">
                                    <select name="levelId" class="address" data-flag="1">
                                        <option value="">请选择</option>
                                        <c:forEach items="${levelList}" var="i">
                                            <option value="${i.id}">${i.name}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-md12">&nbsp;</div>
                            <div class="layui-col-md12">
                                <div class="layui-col-md3">
                                    <div class="layui-col-sm4" style="line-height: 38px">预计释放时间:</div>
                                    <div class="layui-col-sm7">
                                        <input class="layui-input" name="queryTime" id="laydate-cn"
                                               autocomplete="off">
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-col-sm3" style="line-height: 38px">预分红状态</div>
                                    <div class="layui-col-sm7">
                                        <select name="status">
                                            <option value="">请选择</option>
                                            <option value="0" ${status==0?'selected':''}>冻结中</option>
                                            <option value="1" ${status==1?'selected':''}>已发放</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-col-md6">
                                    <div class="layui-col-sm2">
                                        <button class="layui-btn layui-btn" lay-submit
                                                lay-filter="LAY-app-contlist-search" type="submit">查询
                                        </button>
                                    </div>

                                    <div class="layui-col-sm3">
                                        <button class="layui-btn layui-btn" lay-submit
                                                lay-filter="LAY-app-contlist-later" type="submit">批量延迟分红
                                        </button>
                                    </div>

                                    <div class="layui-col-sm3">
                                        <button class="layui-btn layui-btn" lay-submit
                                                lay-filter="LAY-app-contlist-setting" type="submit">设置分红时间
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-col-md12">&nbsp;</div>
                            <div class="layui-col-md12">
                                <div class="layui-col-md8">
                                    <div class="layui-col-sm9" style=" line-height: 38px; font-size: large; ">
                                        时间：<i id="queryTime" style="color: red">${queryTmie}</i> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        预计总分红：<i id="totalMoney" style="color: red">￥${totalMoney}</i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <%-- 数据展示 --%>
                    <div class="layui-col-md12">
                        <div class="layui-col-md8">
                            <div class="layui-col-sm9" style=" line-height: 38px; font-size: large; ">
                                可在下方表格中直接修改<i style="color: red">基金份数</i> 和 <i style="color: red">提鸡券数量</i>
                            </div>
                        </div>
                    </div>
                </div>
                <table class="layui-table" id="table" lay-filter="table"></table>
                <script type="text/html" id="toolbar">
                    <div class="layui-btn-container">
                        {{# if(d.status == 0){ }}
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="execute">手动执行</a>
                        {{# } }}
                    </div>
                </script>
            </div>
        </div>
    </div>
</div>
</div>


<script src="/layuiadmin/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'table'], function () {
        var $ = layui.$, layer = layui.layer, form = layui.form,
            element = layui.element, laydate = layui.laydate, table = layui.table;

        var idarray = [];
        var ids = [];
        form.render('select');
        element.render();
        laydate.render({
            elem: '#laydate-cn',
            range: '~'
        });

        table.render({
            elem: '.layui-table',
            url: '/chickenOrder/querychickenBonusList',
            page: true,
            toolbar: '#table-operate-tool',
            // where:{"size":100},
            request: {
                pageName: 'current', //页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'id', title: '', type: "checkbox"},
                {field: 'nickName', title: '用户昵称'},
                {field: 'levelName', title: '会员等级'},
                {field: 'typeName', title: '分红类型'},
                {field: 'orderCode', title: '订单号'},

                {field: 'chickenNum', title: '基金份数', edit: 'text'},
                // {field: 'price', title: '卖鸡单价(基金单价)'},
                {field: 'couponNum', title: '提鸡券数量', edit: 'text'},
                {field: 'day', title: '孵化期间天数'},
                // {
                //     field: 'createTime', title: '创建时间',
                //     templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm')}}</div>"
                // },
                {
                    field: 'bonusTime', title: '释放时间',
                    templet: "<div>{{layui.util.toDateString(d.bonusTime, 'yyyy-MM-dd HH:mm')}}</div>"
                },
                {
                    field: 'bonusExecuteTime', title: '实际分红时间',
                    templet:
                        function (d) {
                            if (d.bonusExecuteTime == undefined || d.bonusExecuteTime == "undefined" || d.bonusExecuteTime == null) {
                                return "----"
                            } else {
                                return layui.util.toDateString(d.bonusExecuteTime, 'yyyy-MM-dd HH:mm')
                            }
                        }
                },
                {
                    field: 'status', title: '分红记录状态',
                    templet: function (d) {
                        idarray.push(d.id);
                        console.log(idarray)
                        if (d.status == 0) {
                            return '未发放'
                        } else if (d.status == 1) {
                            return ' 已发放'
                        }
                    }
                },
                {field: 'remark', title: '备注'},
                {title: '操作', templet: '#toolbar'}
            ]],
            skin: "row",
            even: true,
            size: "lg"
        });

        var active = {
            detail: function (id) {
                window.location.href = '/chickenOrder/chickenOrderDetail?id=' + id;
            },
            exportThis: function (bid, orderType, payType, orderStatus, phone, orderCode, payCode, queryTime) {
                window.location.href = encodeURI("/export/handleExportOrder?queryTime=" + queryTime + "&payCode=" + payCode + "&orderCode=" + orderCode + "&phone=" + phone + "&payType=" + payType
                    + "&payType=" + payType + "&bid=" + bid + "&orderType=" + orderType + "&orderStatus=" + orderStatus);
            },
            execute: function (id) {
                layer.confirm('确定执行该操作吗？', function () {
                    //---
                    $.ajax({
                        url: "/chickenOrder/handleBonusExecute",
                        data: {'id': id},
                        dataType: "json",
                        type: "post",
                        success: function (data1) {
                            if (data1.code == 0) {
                                layer.msg('操作成功', {icon: 1});
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1000)
                            } else {
                                layer.msg('操作失败', {icon: 2})
                            }
                        }
                    })
                    //---
                });

            }
        };

        $('.backList').on('click', function () {
            history.go(-1);
        });


        //导出事件触发
        $(".exportThis").on('click', function () {
            var exportThis = $(this).data('type');
            var bid = $('select[name="bid"]').find('option:selected').val();
            var orderType = $('select[name="orderType"]').find('option:selected').val();
            var payType = $('select[name="payType"]').find('option:selected').val();
            var orderStatus = $('select[name="orderStatus"]').find('option:selected').val();
            var phone = $('input[name="phone"]').val();
            var orderCode = $('input[name="orderCode"]').val();
            var payCode = $('input[name="payCode"]').val();
            var queryTime = $('input[name="queryTime"]').val();
            active[exportThis] && active[exportThis].call(this, bid, orderType, payType, orderStatus, phone, orderCode, payCode, queryTime);
        });

        //监听事件
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'execute') {// 详情
                active[layEvent] && active[layEvent].call(this, data.id);
            }
        });

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            idarray = [];
            ids = [];
            var field = data.field;
            //执行重载
            table.reload('table', {
                where: field
            });
            //---
            $.ajax({
                url: "/chickenOrder/handleSelectBonusTotalMoney",
                data: {'queryTime': $("[name='queryTime']").val()},
                dataType: "json",
                type: "post",
                success: function (data1) {
                    var map = data1.data;
                    if (data1.code == 0) {
                        $("#queryTime").text(map["queryTmie"])
                        $("#totalMoney").text(map["totalMoney"])

                    } else {
                        $("#queryTime").text(map["queryTime"])
                        $("#totalMoney").text('计算中')
                    }
                }
            })
            //---
        });


        table.on('checkbox(table)', function (obj) {
            console.log(obj.checked); //当前是否选中状态
            console.log(obj.data); //选中行的相关数据
            console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one

            if (obj.type = "one" && obj.checked == true) {
                ids.push(obj.data.id);
            } else if (obj.type = "all" && obj.checked == true) {
                ids = idarray;
            }
        });


        table.on('edit(table)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            console.log(".....................")
            console.log(obj.value); //得到修改后的值
            console.log(obj.field); //当前编辑的字段名
            console.log(obj.data); //所在行的所有相关数据

            //---
            $.ajax({
                url: "/chickenOrder/settingBonus",
                data: {"id": obj.data.id, "chickenNum": obj.data.chickenNum, "couponNum": obj.data.couponNum},
                dataType: "json",
                type: "post",
                success: function (data) {
                    console.log(data)
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }
            })
            //---

        });

        //监听延迟分红
        form.on('submit(LAY-app-contlist-later)', function (data) {
            if (ids.length == 0) {
                layer.msg('请至少选择一项数据', {icon: 1});
                return false;
            }
            layer.open({
                title: '批量延迟分红'
                , content: '<input class="layui-input" name="laterDay" placeholder="输入延迟天数">'
                , yes: function (index, layero) {
                    var laterDay = $("[name='laterDay']").val();
                    if (laterDay == '') {
                        layer.msg('请输入延迟天数', {icon: 1});
                    } else {
                        console.log(ids)
                        $.ajax({
                            url: "/chickenFund/handleLaterDay",
                            data: {ids, "day": laterDay},
                            dataType: "json",
                            type: "post",
                            success: function (data) {
                                console.log(data)
                                if (data.code == 0) {
                                    layer.msg(data.msg, {icon: 1});

                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            }
                        })
                    }

                }
            });
        });

        //设置分红时间
        form.on('submit(LAY-app-contlist-setting)', function (data) {
            if (ids.length != 1) {
                layer.msg('请选择一项数据进行修改', {icon: 1});
                return false;
            }
            layer.open({
                title: '设置分红时间'
                , area: ['420px', '240px'] //宽高
                , content: ' <input class="layui-input" name="newBonusTime"  id="laydate-cn2" autocomplete="off">',
                success: function (layero, index) {
                    layui.config({
                        base: '/layuiadmin/' //静态资源所在路径
                    }).extend({
                        index: 'lib/index' //主入口模块
                    }).use(['laydate'], function () {
                        var laydate = layui.laydate;
                        console.log(laydate)
                        laydate.render({
                            elem: '#laydate-cn2',
                            type: 'datetime'
                        });
                    })
                }
                , yes: function (index, layero) {
                    var newBonusTime = $("[name='newBonusTime']").val();
                    if (newBonusTime == '') {
                        layer.msg('请输入分红时间', {icon: 1});
                    } else {
                        $.ajax({
                            url: "/chickenOrder/settingBonus",
                            data: {"id": ids[0], "bonusTimeStr": newBonusTime},
                            dataType: "json",
                            type: "post",
                            success: function (data) {
                                console.log(data)
                                if (data.code == 0) {
                                    layer.msg(data.msg, {icon: 1});
                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            }
                        })
                    }

                }
            });
        });
    })


</script>
</body>
</html>